package com.example.primomobile;

import java.util.HashMap;
import java.util.Map;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnDragListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.primomobile.utils.Utils;

public class TabuleiroActivity extends Fragment {
	private View rootView;
	private GridView gridview;
	private ImageAdapter imageAdapter;
	private Context context;
	private LinearLayout linearLayout;

	@Override
	public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
		rootView = inflater.inflate( R.layout.activity_tabuleiro, container, false );

		context = rootView.getContext();
		View imgFrente = rootView.findViewById( R.id.img_frente );
		View imgEsquerda = rootView.findViewById( R.id.img_esquerda );
		View imgDireita = rootView.findViewById( R.id.img_direita );
		View imgFuncao = rootView.findViewById( R.id.img_funcao );
		ImageButton buttonPlay = (ImageButton) rootView.findViewById( R.id.btn_info );
		buttonPlay.setOnClickListener( new Button.OnClickListener() {

			@Override
			public void onClick( View v ) {
				save( v );
			}
		} );

		imgDireita.setOnTouchListener( new PecasTouchListener() );
		imgEsquerda.setOnTouchListener( new PecasTouchListener() );
		imgFrente.setOnTouchListener( new PecasTouchListener() );
		imgFuncao.setOnTouchListener( new PecasTouchListener() );

		Map<String, Object> map;
		map = new HashMap<String, Object>();
		map.put( Utils.REMOVE, false );
		map.put( Utils.PECA, Utils.FRENTE );
		map.put( Utils.POSICAO, 0 );
		imgFrente.setTag( map );

		map = new HashMap<String, Object>();
		map.put( Utils.REMOVE, false );
		map.put( Utils.PECA, Utils.ESQUERDA );
		imgEsquerda.setTag( map );

		map = new HashMap<String, Object>();
		map.put( Utils.REMOVE, false );
		map.put( Utils.PECA, Utils.DIREITA );
		imgDireita.setTag( map );

		map = new HashMap<String, Object>();
		map.put( Utils.REMOVE, false );
		map.put( Utils.PECA, Utils.FUNCAO );
		imgFuncao.setTag( map );

		gridview = (GridView) rootView.findViewById( R.id.content );
		imageAdapter = new ImageAdapter( context );
		gridview.setAdapter( imageAdapter );

//		rootView.findViewById( R.id.menu ).setOnDragListener( new PecasRemoveDragListener( imageAdapter ) );

		gerarAreaFuncao();
		return rootView;
	}

	public void gerarAreaFuncao() {
		linearLayout = (LinearLayout) rootView.findViewById( R.id.content_function );
		for ( int i = 0; i < 4; i++ ) {

			ImageView pecaPadrao = new ImageView( context );
			pecaPadrao.setImageResource( R.drawable.img_default1 );
			pecaPadrao.setOnTouchListener( new RemoverPecaListener() );
			pecaPadrao.setLayoutParams( new GridView.LayoutParams( 120, 120 ) );
			pecaPadrao.setScaleType( ImageView.ScaleType.CENTER_CROP );
			pecaPadrao.setPadding( 0, 4, 0, 4 );
			pecaPadrao.setOnDragListener( adicionarPecaListener );
			Map<String, Object> tag = new HashMap<String, Object>();
			tag.put( Utils.REMOVE, false );
			tag.put( Utils.PECA_TROCA, false );
			pecaPadrao.setTag( tag );
			linearLayout.addView( pecaPadrao );
		}
	}

	OnDragListener adicionarPecaListener = new OnDragListener() {

		@Override
		public boolean onDrag( View view, DragEvent event ) {
			switch ( event.getAction() ) {
				case DragEvent.ACTION_DRAG_STARTED:
					break;
				case DragEvent.ACTION_DRAG_ENTERED:
					break;
				case DragEvent.ACTION_DRAG_EXITED:
					break;
				case DragEvent.ACTION_DROP:
					ImageView peca = (ImageView) event.getLocalState();
					Object tag = peca.getTag();
					if ( tag != null ) {
						Map pecaDestino = (Map) view.getTag();
						Map pecaOrigem = (Map) tag;

						if ( pecaOrigem.containsKey( Utils.PECA_TROCA ) && ( pecaDestino != null && (Boolean) pecaDestino.get( Utils.REMOVE ) ) ) {
							Object pecaNova = pecaOrigem.get( Utils.PECA );

							pecaOrigem.put( Utils.PECA, pecaDestino.get( Utils.PECA ) );
							pecaOrigem.put( Utils.REMOVE, true );
							pecaOrigem.put( Utils.PECA_TROCA, true );

							pecaDestino.put( Utils.PECA, pecaNova );
							if ( pecaOrigem.containsKey( Utils.POSICAO ) ) {
								Utils.getImageParaTabuleiro( (Integer) pecaOrigem.get( Utils.POSICAO ), peca );
							} else {
								Object param = pecaOrigem.get( Utils.PECA );
								if ( param.equals( Utils.DIREITA ) ) {
									peca.setImageResource( R.drawable.direita1 );
								} else if ( param.equals( Utils.ESQUERDA ) ) {
									peca.setImageResource( R.drawable.esquerda1 );
								} else if ( param.equals( Utils.FRENTE ) ) {
									peca.setImageResource( R.drawable.frente1 );
								} else if ( param.equals( Utils.FUNCAO ) ) {
									peca.setImageResource( R.drawable.funcao1 );
								}
							}
						} else {
							pecaDestino.put( Utils.PECA, pecaOrigem.get( Utils.PECA ) );
						}

						pecaDestino.put( Utils.REMOVE, true );
						pecaDestino.put( Utils.PECA_TROCA, true );

						if ( ( (Map) view.getTag() ).containsKey( Utils.PECA ) ) {
							String p = (String) ( (Map) view.getTag() ).get( Utils.PECA );
							if ( p.equals( Utils.DIREITA ) ) {
								( (ImageView) view ).setImageResource( R.drawable.direita1 );
							} else if ( p.equals( Utils.ESQUERDA ) ) {
								( (ImageView) view ).setImageResource( R.drawable.esquerda1 );
							} else if ( p.equals( Utils.FRENTE ) ) {
								( (ImageView) view ).setImageResource( R.drawable.frente1 );
							} else if ( p.equals( Utils.FUNCAO ) ) {
								( (ImageView) view ).setImageResource( R.drawable.funcao1 );
							}
						}
					}

					break;
				case DragEvent.ACTION_DRAG_ENDED:
				default:
					break;
			}
			return true;
		}
	};

	public void clean( View view ) {
		Toast.makeText( context, "Limpar", Toast.LENGTH_SHORT ).show();
		imageAdapter.clean();

	}

	public void save( View view ) {
		String funcao = "";
		for ( int i = 0; i < linearLayout.getChildCount(); i++ ) {
			View v = linearLayout.getChildAt( i );
			Map tag = ((Map)v.getTag());
			boolean remove= (Boolean) tag.get( Utils.REMOVE );
			boolean troca= (Boolean) tag.get( Utils.PECA_TROCA );
			if(remove && troca){
				String peca = (String) tag.get( Utils.PECA );
				if(peca.equals( Utils.FRENTE )){
					funcao = funcao.concat("F");
				}else if(peca.equals( Utils.ESQUERDA )){
					funcao = funcao.concat("E");
				}else if(peca.equals( Utils.DIREITA )){
					funcao = funcao.concat("D");
				}else if(peca.equals( Utils.FUNCAO )){
				}
			}
		}
		String execultar = imageAdapter.getPecas().replaceAll( "-", funcao );
		Toast.makeText( context, execultar, Toast.LENGTH_LONG ).show();
		Log.d( "Programa", execultar );
	}

	public void run( View view ) {
		Toast.makeText( context, "RUN", Toast.LENGTH_SHORT ).show();
	}

}